﻿using System;
using System.Collections.Generic;
using Besture.UI.ClassHelper;
using Besture.Utils;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using Besture.Common;
using Besture.Bll;

namespace Besture.UI.Admin.Help
{
    public partial class HelpList: BasePage
    {
        protected string type = Fun.Query("type");
        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            type = string.IsNullOrEmpty(type) ? "0" : "1";
            btnAdd.Text = string.IsNullOrEmpty(type) ? "添加公告" : "添加帮助";
            BindData(type);
            AspxGridViewHelper.InitAspxGridView(grid);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData(type);
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData(string type)
        {
            HelpDac newsDac = new HelpDac();
            grid.DataSource = newsDac.GetDataTable(type);
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }


        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            int ID = Convert.ToInt32(e.Keys[0]);
            HelpDac newsDac = new HelpDac();
            newsDac.Delete(ID);
            BindData(type);
        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            HelpDac newsDac = new HelpDac();
            newsDac.DeleteList(ids);
            BindData(type);
        }

        /// <summary>
        /// 获得多选框ID
        /// </summary>
        /// <returns></returns>
        private string GetCheckID()
        {
            string rValue = "";
            try
            {
                List<object> values = grid.GetSelectedFieldValues(new[] { grid.KeyFieldName });
                List<string> str = new List<string>(values.Count); //初始化
                for (int i = 0; i < values.Count; i++)
                    str.Add(values[i].ToString());
                foreach (string tempStr in str)
                    rValue += tempStr + ",";

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rValue;
        }
 
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="eventArgs"></param>
        protected void btnAdd_Click(object obj, EventArgs eventArgs)
        {
            if (string.IsNullOrEmpty(type))
            {
                RedirectUrl("HelpSet.aspx");
            }
            else
            {
                RedirectUrl("HelpSet.aspx?type=1");
            }
        }

    }
}